package utils;

import services.impl.RedisService;

public class RedisUtil {
	public static String CACHE_SPLIT_CHAR = "#";
	public static int AUTHERIZE_CACHE_EX = 1800;
	public static int NO_CACHE_EX = 0;
	
	/**
	 * @param redis
	 * @param key
	 * 
	 * source method: Cache jfinalCache = Redis.use("fujing");
	 *				  jfinalCache.del(key);
	 */
	public static void clearCacheByKey(RedisService redis, Object key){
		redis.remove(buildCacheName(redis), key);
	}
	
	public static void clearAllCache(RedisService redis){
		redis.removeAll(buildCacheName(redis));
	}
	
	/**
	 * @param redis
	 * @param cacheEx
	 * @param key
	 * @param value
	 * 
	 * source method: Cache jfinalCache = Redis.use("fujing");
	 *				  jfinalCache.setex(userId, 1800, token);
	 */
	public static void addCache(RedisService redis,int cacheEx, Object key, Object value){
		redis.put(buildCacheName(redis,cacheEx), key, value);
	}
	
	public static void addCache(RedisService redis, Object key, Object value){
		redis.put(buildCacheName(redis), key, value);
	}
	
	private static String buildCacheName(RedisService redis,int cacheEx){
		String cacheName = redis.getCacheName()+CACHE_SPLIT_CHAR+cacheEx;
		return cacheName;
	}
	
	private static String buildCacheName(RedisService redis){
		String cacheName = redis.getCacheName()+CACHE_SPLIT_CHAR;
		return cacheName;
	}
	
	public static <T> T get(RedisService redis,String key){
		return redis.get(buildCacheName(redis), key);
	}

}
